package com.ly.lypopupwindowdemo;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by liying on 2018/1/3.
 */

public class BaseTitle extends RelativeLayout{
    private String titleStr, rightStr;
    private boolean showRightText, showRightImg;//是否显示右上角的图标，默认不显示
    private boolean showBack;//是否显示返回按钮，默认显示
    private int resourceImg;

    private RelativeLayout backLayout, rightLayout;
    private TextView rightText;
    private ImageView rightImg;
    private TextView textView;
    private Drawable drawable;
    private static final int DEFAULT_BORDER_COLOR = Color.WHITE;
    private int rightColor = DEFAULT_BORDER_COLOR;
    private int bgColor;

    private RelativeLayout title_bg_rl;

    public BaseTitle(Context context) {
        this(context, null);
    }

    public BaseTitle(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseTitle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BaseTitle);
        titleStr = ta.getString(R.styleable.BaseTitle_title_text);
        showRightText = ta.getBoolean(R.styleable.BaseTitle_show_right_text, false);
        showRightImg = ta.getBoolean(R.styleable.BaseTitle_show_right_img, false);
        showBack = ta.getBoolean(R.styleable.BaseTitle_show_back, true);
        rightStr = ta.getString(R.styleable.BaseTitle_right_text);
        drawable = ta.getDrawable(R.styleable.BaseTitle_right_img);
        rightColor = ta.getColor(R.styleable.BaseTitle_right_color, DEFAULT_BORDER_COLOR);
        bgColor = ta.getColor(R.styleable.BaseTitle_bg_color, getResources().getColor(R.color.google_blue));

//        ta.getValue(R.styleable.BaseTitle_right_img, TypedValue.TYPE_REFERENCE);
        ta.recycle();

        View vi = LayoutInflater.from(context).inflate(R.layout.activity_base_title, null);
        addView(vi);
        backLayout = (RelativeLayout) vi.findViewById(R.id.id_base_title_back);
        textView = (TextView) vi.findViewById(R.id.id_base_title_title);
        rightImg = (ImageView) vi.findViewById(R.id.id_base_title_right_img);
        rightText = (TextView) vi.findViewById(R.id.id_base_title_right_text);
        rightLayout = (RelativeLayout) vi.findViewById(R.id.id_base_title_right);

        title_bg_rl = (RelativeLayout) vi.findViewById(R.id.title_bg);

        backLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (leftClick == null) {
                    ((Activity) getContext()).finish();
                    //((Activity) getContext()).overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
                } else {
                    leftClick.click();
                }
            }
        });

        if (resourceImg != 0) {
            rightImg.setImageResource(resourceImg);
        }
        textView.setText(titleStr);
        textView.setTextSize(18);
        rightText.setText(rightStr);
        rightText.setTextSize(13);
        rightText.setTextColor(rightColor);
        title_bg_rl.setBackgroundColor(bgColor);
        if (!showRightText) {
            rightText.setVisibility(INVISIBLE);
        }
        if (!showRightImg) {
            rightImg.setVisibility(INVISIBLE);
        } else {
//            rightImg.setImageResource();
            rightImg.setImageDrawable(drawable);
        }
        if (!showBack) {
            backLayout.setVisibility(INVISIBLE);
        }

        rightLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (click != null) {
                    click.click();
                }
            }
        });
    }

    RightOnClick click;
    LeftOnClick leftClick;

    public interface RightOnClick {
        void click();
    }

    public void setRightOnClickLinstner(RightOnClick clickLinstner) {
        this.click = clickLinstner;
    }

    public void setTitleStr(String title) {
        this.textView.setText(title);
    }

    public void setRightStr(String title) {
        this.rightText.setText(title);
    }

    public void setShowBack(boolean showBack) {
        this.showBack = showBack;
        if (showBack) {
            backLayout.setVisibility(VISIBLE);
        } else {
            backLayout.setVisibility(INVISIBLE);
        }
    }

    public interface LeftOnClick {
        void click();
    }

    public void setLeftOnClickListener(LeftOnClick leftClick) {
        this.leftClick = leftClick;
    }


    public String getRightStr() {

        return rightText.getText().toString();
    }

    public void setBgColor(int color) {
        bgColor = color;
        title_bg_rl.setBackgroundColor(bgColor);
    }

}
